<?php
App::uses('AppController', 'Controller');
/**
 * News Controller
 *
 * @property News $News
 * @property PaginatorComponent $Paginator
 */
class NewsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->News->recursive = 0;
        $this->Paginator->settings= array("limit"=>5,'order'=>array('News.update_date DESC'));
		$this->set('news', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function info($id = null) {
		$title_for_layout= " Tin tức khuyến mãi, tin thời trang";
		if (!$this->News->exists($id)) {
			throw new NotFoundException(__('Invalid news'));
		}
		$options = array('conditions' => array('News.' . $this->News->primaryKey => $id));
		$this->set('news', $this->News->find('first', $options));
		$this->set(compact('title_for_layout'));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->News->create();
            $this->request->data['News']['create_date']= date("Y-m-d");
           	if ($this->News->save($this->request->data)) {
				$this->Session->setFlash(__('Thêm thành công '),'backend/green',array('redirect'=>''));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Lỗi save dữ liệu'),'backend/red',array('redirect'=>''));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->News->exists($id)) {
			throw new NotFoundException(__('Invalid news'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
		  if ($this->News->save($this->request->data)) {
					$this->Session->setFlash(__('Sửa thành công '),'backend/green',array('redirect'=>''));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Lỗi save dữ liệu'),'backend/red',array('redirect'=>''));
			}
		} else {
			$options = array('conditions' => array('News.' . $this->News->primaryKey => $id));
			$this->request->data = $this->News->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->News->id = $id;
		if (!$this->News->exists()) {
			throw new NotFoundException(__('Invalid news'));
		}
		$this->request->onlyAllow('get', 'admin_delete');
		if ($this->News->delete()) {
			$this->Session->setFlash(__('Xóa thành công'),'backend/green',array('redirect'=>''));
		} else {
			$this->Session->setFlash(__('Lỗi xóa dữ liệu'),'backend/red',array('redirect'=>''));
		}
		return $this->redirect(array('action' => 'admin_index'));
	}
        public function getNews()
        {
            $news = $this->News->find('all',array('order'=>'create_date desc'));
            return $news;
        }
                }
